﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SacMauVN.Admin
{
    public partial class ContentDetail : System.Web.UI.Page
    {
        private static int mod = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadContent();
                MultiView1.ActiveViewIndex = 1;
            }
        }

        protected void LoadContent()
        {
            string sql = "SELECT * FROM dbo.tblSMVNContent WHERE Flag = 1";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            grvData.DataSource = dt;
            grvData.DataBind();

        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            btnLuu.CommandName = MyConst.Action.Addnew;
            MultiView1.ActiveViewIndex = 0;
        }

        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string sID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;

            if ((sCommandName == SacMauVN.MyConst.Action.Approve))
            {
                btnLuu.CommandName = MyConst.Action.Approve;
                GetChiTietNoiDung(sID);
                mod = Convert.ToInt32(sID);
                MultiView1.ActiveViewIndex = 0;
            }
            if ((sCommandName == MyConst.Action.Delete))
            {
                string sql = "DELETE FROM dbo.tblSMVNContent WHERE ID = " + sID;
                SqlHelper.ExecuteNonQuery(CommandType.Text, sql);
            }
            grvData.EditIndex = -1;
            LoadContent();
        }

        private void GetChiTietNoiDung(string sID)
        {
            string sql = "SELECT * FROM dbo.tblSMVNContent WHERE ID = @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, sID));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count > 0)
            {
                txtTen.Text = dt.Rows[0]["TenDM"].ToString();
                txtContent.Value = dt.Rows[0]["NoiDung"].ToString();
            }
        }

        protected void grvData_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvData.EditIndex = e.NewEditIndex;
            LoadContent();
        }

        protected void grvData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvData.EditIndex = -1;
            LoadContent();
        }

        protected void grvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            grvData.DeleteRow(e.RowIndex);
            grvData.DataBind();
        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            if (btnLuu.CommandName == MyConst.Action.Addnew)
            {
                ThemNoiDung();
            }
            else
            {
                SuaNoiDung();
            }
            LoadContent();
            MultiView1.ActiveViewIndex = 1;
        }

        private void ThemNoiDung()
        {
            //string sql = "INSERT INTO dbo.tblSMVNContent(TenDM ,NoiDung ,CreateDate)" +
            //            "VALUES  (@MaDM,@TenDM,@NoiDung,GETDATE())";
            //List<SqlParameter> arrParams = new List<SqlParameter>();
            //arrParams.Add(SqlHelper.CreateParameterObject("@TenDM", SqlDbType.NVarChar, ParameterDirection.Input, txtTen.Text));
            //arrParams.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NText, ParameterDirection.Input, txtContent.Value));
            //SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
            //MultiView1.ActiveViewIndex = 1;
        }

        private void SuaNoiDung()
        {
            string sql = "UPDATE dbo.tblSMVNContent SET NoiDung = @NoiDung WHERE ID = @ID";
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@ID", SqlDbType.Int, ParameterDirection.Input, mod));
            arrParams.Add(SqlHelper.CreateParameterObject("@NoiDung", SqlDbType.NText, ParameterDirection.Input, txtContent.Value));
            SqlHelper.ExecuteNonQuery(CommandType.Text, sql, arrParams);
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
        }
    }
}
